package com.aaa.controller;

import com.aaa.service.OrderService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("order")
public class OrderController {

    @Resource
    OrderService orderService;

    @RequestMapping("selectOrderId")
    @ResponseBody
    public Integer selectOrderId(){
        return 0;
    }

//  查询订单
    @RequestMapping("selectOrder")
    @ResponseBody
    public List<Map<String,Object>> selectOrder(Integer id){
        return orderService.selectOrder(id);
    }

//   取消订单
    @RequestMapping("removeOrder")
    @ResponseBody
    public boolean removeOrder(Integer id,String order_sn){
        Integer rs1=orderService.removeOrProduct(order_sn);
        Integer rs=0;
        if (rs1>0){
            rs=orderService.removeOrderId(id,order_sn);
        }
        return rs==0?false:true;
    }

    @RequestMapping("addOrder")
    @ResponseBody
    public Map<String,Object> addOrder(String id,String username,String address,
                                       String name,String phone,String countPrice){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String orderid1=sdf.format(date);
        String orderid2=orderid1.replace("-","");
        System.out.println(orderid2);
        for (int i=0;i<10;i++){
            orderid2+=(int)(Math.random()*10);
        }
        System.out.println(orderid2+"  ----");
        Integer rs=orderService.addOrder(id,orderid2,username,address,name,phone,countPrice);
        if (rs>0){
            Map<String,Object> map=orderService.selectOrderByOrderId(orderid2);
            Map<String,Object> mapList=new HashMap<>();
            mapList.put("order_sn",orderid2);
            mapList.put("id",map.get("id"));
            return mapList;
        }
        return null;
    }

    @RequestMapping("addOrderItem")
    @ResponseBody
    public boolean addOrderItem(String ids,String order_sn,String oid){
        String[] id=ids.split(",");
        String rs1="";
        String rs2="";
        for (int i=0;i<id.length;i++){
            Map<String,Object> map=orderService.selectCartByOrderId(id[i]);
            Integer rs=orderService.addOrderItem(oid,order_sn,map.get("product_id")
            ,map.get("product_name"),map.get("price"),map.get("quantity"));
            rs1+=1;
            rs2+=rs;
        }
        if (rs1.equals(rs2)){
            for (int i=0;i<id.length;i++){
                Integer rs=orderService.removeCartById(id[i]);
                rs1+=1;
                rs2+=rs;
            }
            if (rs1.equals(rs2)){
                return true;
            }else {
                return false;
            }
        }else {
            orderService.removeByOrderSn(order_sn);
            return false;
        }
    }

    @RequestMapping("selectOrderById")
    @ResponseBody
    public List<Map<String,Object>> selectOrderById(String id){
        return orderService.selectOrderById(id);
    }

}
